package paidaxing.blog.common.utils;

import cn.hutool.core.util.StrUtil;
import paidaxing.blog.common.exception.BusinessException;
import paidaxing.blog.common.result.ResultCode;

import java.util.Collection;
import java.util.Map;

/**
 *参数校验工具类
 *
 * @author heefM
 * @date 2025-10-17
 */
public class ValidationUtils {

    /**
     * 校验对象不为空
     */
    public static void notNull(Object obj, String message) {
        if (obj == null) {
            throw new BusinessException(ResultCode.VALIDATE_FAILED, message);
        }
    }

    /**
     * 校验字符串不为空
     */
    public static void notBlank(String str, String message) {
        if (StrUtil.isBlank(str)) {
            throw new BusinessException(ResultCode.VALIDATE_FAILED, message);
        }
    }

    /**
     * 校验集合不为空
     */
    public static void notEmpty(Collection<?> collection, String message) {
        if (collection == null || collection.isEmpty()) {
            throw new BusinessException(ResultCode.VALIDATE_FAILED, message);
        }
    }

    /**
     * 校验Map不为空
     */
    public static void notEmpty(Map<?, ?> map, String message) {
        if (map == null || map.isEmpty()) {
            throw new BusinessException(ResultCode.VALIDATE_FAILED, message);
        }
    }

    /**
     * 校验条件为true
     */
    public static void isTrue(boolean condition, String message) {
        if (!condition) {
            throw new BusinessException(ResultCode.VALIDATE_FAILED, message);
        }
    }

    /**
     * 校验条件为false
     */
    public static void isFalse(boolean condition, String message) {
        if (condition) {
            throw new BusinessException(ResultCode.VALIDATE_FAILED, message);
        }
    }

    /**
     * 校验字符串长度
     */
    public static void checkLength(String str, int min, int max, String fieldName) {
        if (StrUtil.isBlank(str)) {
            return;
        }
        int length = str.length();
        if (length < min || length > max) {
            throw new BusinessException(ResultCode.VALIDATE_FAILED,
                    fieldName + "长度必须在" + min + "到" + max + "之间");
        }
    }
}